/**
 * Created by Administrator on 2017/8/16.
 */
$(function(){
var key = getCookie('key');
var type = getQueryString('type');
$('#type').val(type);
var btns = document.getElementById('btn');
$.ajax({
    type:"POST",
    url:ApiUrl+"/index.php?act=article&op=articleType",
    data:{key:key,type:type},
    dataType:"json",
        success:function(data){
        var data = data.datas.data;
        offset = '';
         if(data['class'].class_id==1) {
             offset = 2;
         }else{
             offset = 1;
         }
        $("#areaSelected"+offset).remove();
        $('#author').html(data['member']['member_name']);
        $("#class").val(data['class'].class_name);$("#class").attr('class_id',data['class'].class_id);
    }
})
    // 选择地区
    $('#area_info').on('click', function(){
        $.areaSelected({
            success : function(data){
                $('#area_info').val(data.area_info).attr({'data-areaid':data.area_id,'data-areaid1':data.area_id_1 , 'data-areaid2':(data.area_id_2 == 0 ? data.area_id_1 : data.area_id_2),'data-areaid3':(data.area_id_3 == 0 ? data.area_id_2 : data.area_id_3)});
            }
        });
    });

btns.onclick = function(){
    var title = document.getElementById('title').value;
    var sort_title = document.getElementById('sort_title').value;
    var content = document.getElementById('content').innerHTML;
    var id = document.getElementById('class').getAttribute('class_id');
    var author = document.getElementById('author').innerHTML;
    if (document.getElementById('cid')){
        var cid = document.getElementById('cid').value();
    }

    var pic = document.getElementById('view').src;
    var pics = [];
    pics.push(pic);
    if(title==''){
        creatLayout("请填写标题");
        return;
    }
    if(content==''){
        creatLayout("请填写内容");
        return;
    }
    //提交的数据
    var data = {
        article_class:id,
        article_content:content,
        article_title_short:sort_title,
        title:title,
        article_author:author,
        thumb:pics,
        key:key,
    }
    if(id==2){
        var fname = document.getElementById('firstname');
        var ab = document.getElementById('ab');
        data['firstname'] = fname.value;
        data['abbreviate'] = ab.value;
    }else{
        var address = document.getElementById('area_info');
        data['province_id'] = address.getAttribute('data-areaid1');
        data['city_id'] = address.getAttribute('data-areaid2');
        data['area_id'] = address.getAttribute('data-areaid3');
        data['region'] = address.value;
     }

    if(cid){
        data['id'] = cid;
    }
    $.ajax({
        type:"POST",
        url:ApiUrl+"/index.php?act=article&op=article_add",
        data:data,
        dataType:"json",
        success:function(data){
            if(data['code']==200){
                data = data.datas;
                if(data['status']) {
                    window.location.href = WapSiteUrl + "/tmpl/article.html";
                }else{
                    creatLayout('发布失败');
                }
            }else{
                creatLayout('发布失败');
            }
        }
    })

}

//创建提示框
function creatLayout(Html){
    var Layout = document.createElement('div');
    Layout.className = 'code_tips';
    Layout.innerHTML = Html;
    document.body.appendChild(Layout);
    Move(Layout,'opacity',100);
    setTimeout(function(){
        Move(Layout,'opacity',0);
        setTimeout(function(){
            document.body.removeChild(Layout);
        },500)
    },1000)
}
})